﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Utils;
using System.Data.SqlClient;
using System.Data;

namespace AlertSystem.Business
{
    public partial class SysMenu
    {
        #region Singletion pattern
        private static List<SysMenu> _instance;
        private static readonly object _lock = new object();
        public static List<SysMenu> SingletonInstance
        {
            get
            {
                if (_instance == null)
                {
                    lock (_lock)
                    {
                        using (var context = new DBDataContext(AppSetting.ConnectionString))
                        {
                            _instance = context.SysMenus.ToList();
                        }
                    }
                }
                return _instance;
            }
        }

        public static void ReloadData()
        {
            lock (_lock)
            {
                if (_instance != null)
                    _instance.Clear();
                else
                    _instance = new List<SysMenu>();
                using (var context = new DBDataContext(AppSetting.ConnectionString))
                {
                    _instance = context.SysMenus.ToList();
                }
            }
        }
        #endregion Singleton pattern

        /// <summary>
        /// Lấy danh sách menu theo người dùng.
        /// </summary>
        /// <param name="userID">ID của người dùng đang đăng nhập</param>
        /// <returns></returns>
        public static List<SysMenu> GetByUser(int userID)
        {
            try
            {
                using (var context = new DBDataContext(AppSetting.ConnectionString))
                {
                    return context.usp_SysMenu_GetByUser(userID).ToList();
                }
            }
            catch (Exception e)
            {
                throw e;
            }
        }

        public static string GetDefaultMenu(int UserID)
        {
            List<SysMenu> list = GetByUser(UserID);
            if (list.Count == 2)
                return list[1].Link;
            else
                return "";
        }

        public static SysMenu GetByID(int menuId)
        {
            using (var context = new DBDataContext(AppSetting.ConnectionString))
            {
                return context.SysMenus.FirstOrDefault(p => p.MenuID == menuId);
            }
        }

        public static List<SysMenu> GetAll()
        {
            using (var context = new DBDataContext(AppSetting.ConnectionString))
            {
                return context.SysMenus.Where(p => p.Disabled == false).OrderBy(p => p.Level).ToList();
            }
        }

        /// <summary>
        /// Lấy tập menu để phân quyền theo user
        /// </summary>
        public static DataTable GetByUserID(int userID)
        {
            DataTable retTable = new DataTable();
            retTable.Columns.Add("MenuID", typeof(int));
            retTable.Columns.Add("MenuName", typeof(string));
            foreach (var item in SysRole.GetAll())
            {
                retTable.Columns.Add(item.RoleCode, typeof(string));
            }
            foreach (var item in SysMenu.GetAll())
            {
                DataRow row = retTable.Rows.Add(item.MenuID, item.MenuName.ShowNameLevel(item.Level));
                foreach (DataColumn col in retTable.Columns)
                {
                    if (col.ColumnName != "MenuID" && col.ColumnName != "MenuName" && col.ColumnName != "Level")
                    {
                        if (SysUserRoleMenu.CheckExists(userID, item.MenuID, col.ColumnName))
                            row[col] = true;
                        else
                            row[col] = false;
                    }
                }
            }
            return retTable;
        }
        /// <summary>
        /// Lấy tập menu để phân quyền theo Group
        /// </summary>
        public static DataTable GetByGroupID(int groupId)
        {
            DataTable retTable = new DataTable();
            retTable.Columns.Add("MenuID", typeof(int));
            retTable.Columns.Add("MenuName", typeof(string));
            foreach (var item in SysRole.GetAll())
            {
                retTable.Columns.Add(item.RoleCode, typeof(string));
            }
            foreach (var item in SysMenu.GetAll())
            {
                DataRow row = retTable.Rows.Add(item.MenuID, item.MenuName.ShowNameLevel(item.Level));
                foreach (DataColumn col in retTable.Columns)
                {
                    if (col.ColumnName != "MenuID" && col.ColumnName != "MenuName" && col.ColumnName != "Level")
                    {
                        if(SysGroupRoleMenu.CheckExists(groupId, item.MenuID, col.ColumnName))
                            row[col] = true;
                        else
                            row[col] = false;
                    }
                }
            }
            return retTable;
        }

        public static string GetNameByLevel(string name, string level)
        {
            string retValue = name.ShowNameLevel(level);
            if (!retValue.StartsWith("---"))
                retValue = string.Format("<b>{0}</b>", retValue);
            return retValue;
        }
    }
}
